/**
 * 公共部分语言翻译处理
 * 依赖：custom/plugins/yufp.service.js
 * created by helin3 2017-12-04
 */
(function (yufp, window, factory) {
  var exports = factory(yufp, window, window.document);
  if (typeof define === 'function') {
    define(exports);
  }
  window.yufp.language = exports;
}(yufp, window, function (yufp, window, document) {
  /**
     * 统一数据字典管理器
     * @constructor
     */
  function Language () {
  }
  /**
   * 获取对应的翻译文本
   * @param {String} key 待转换的key
   * @param {Object} ops 格式化字符串
   */
  Language.prototype.getLocaleText = function (key, ops, count) {
    return resetTemplate(getValueByPath(window['lang' + window.YUFP_SYS_CONFIG.language.substr(0, 1).toLocaleUpperCase() + window.YUFP_SYS_CONFIG.language.substring(1)], key), ops, count);
  };
  /**
   * 格式化模板
   * @param {string} template 模板
   * @param {Object} ops 模板中对应的key值对
   */
  function resetTemplate (template, ops, count) {
    var num = count || 0;
    var temp = ops && template ? template.replace(/{\w*}/g, function (node, key, index) {
      return ops[num++] || '';
    }) : template;
    return count == undefined ? temp : {
      count: num,
      temp: temp
    };
  }
  /**
   * 根据属性在对象中取值
   * @param {Object} object 取值对象
   * @param {String} prop 属性
   */
  function getValueByPath (object, prop) {
    prop = prop || '';
    var paths = prop.split('.');
    var current = object;
    var result = null;
    for (var i = 0, j = paths.length; i < j; i++) {
      var path = paths[i];
      if (!current) {
        break;
      }
      if (i === j - 1) {
        result = current[path];
        break;
      }
      current = current[path];
    }
    return result;
  };
  return new Language();
}));
